/*
 * TerminarTiempoRegistroAyudantia.java
 *
 * Created on August 20, 2007, 7:08 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package hero.hook;

import hero.interfaces.BnNodeLocal;
import hero.interfaces.BnNodeValue;
import hero.interfaces.Constants;
import hero.interfaces.ProjectSession;
import hero.interfaces.ProjectSessionHome;
import hero.interfaces.ProjectSessionUtil;
import hero.interfaces.UserSession;
import hero.interfaces.UserSessionHome;
import hero.interfaces.UserSessionUtil;
import hero.util.HeroException;
import hero.util.HeroHookException;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.naming.NamingException;

/**
 *
 * @author kabad
 */
public class TerminarTiempoRegistroAyudantia implements hero.hook.NodeHookI {

    /**
     * Creates a new instance of TerminarTiempoRegistroAyudantia
     */
    public TerminarTiempoRegistroAyudantia() {
             System.out.println("TerminarTiempoRegistroAyudantia");

    }

    public String getMetadata() {
        return Constants.Nd.ONDEADLINE;
    }

    public void create(Object b, BnNodeLocal n) throws HeroHookException {
    }

    public void beforeStart(Object b, BnNodeLocal n) throws HeroHookException {
    }

    public void afterTerminate(Object b, BnNodeLocal n) throws HeroHookException {
    }

    public void onCancel(Object b, BnNodeLocal n) throws HeroHookException {
    }

    public void anticipate(Object b, BnNodeLocal n) throws HeroHookException {
    }

    public void onDeadline(Object b, BnNodeLocal n) throws HeroHookException {
        ProjectSessionHome prjHome;
        BnNodeValue actividad;
        UserSession usrSession;
        UserSessionHome usrHome;

        System.out.println("xxSe termino la convocatoria para ayudantes, instancia:" + n.getBnProject().getName());


        try {
            usrHome = UserSessionUtil.getHome();
            usrSession = usrHome.create();

            prjHome = (ProjectSessionHome) ProjectSessionUtil.getHome();
            ProjectSession prjSession = prjHome.create();

            actividad = usrSession.getNode(n.getBnProject().getName(), "Registro de Estudiantes para Ayudantia");
            prjSession.initProject(n.getBnProject().getName());
            prjSession.setNodeType("Registro de Estudiantes para Ayudantia", hero.interfaces.Constants.Nd.AND_JOIN_AUTOMATIC_NODE);
                                                                        
            if (actividad != null &&
                    (actividad.getState() == hero.interfaces.Constants.Nd.READY ||
                    actividad.getState() == hero.interfaces.Constants.Nd.ANTICIPABLE)) {
                usrSession.startActivity(n.getBnProject().getName(), "Registro de Estudiantes para Ayudantia");
                
            }
        } catch (RemoteException ex) {
            ex.printStackTrace();
        } catch (CreateException ex) {
            ex.printStackTrace();
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (HeroException ex) {
            ex.printStackTrace();
        }
    }

    public void afterStart(Object b, BnNodeLocal n) throws HeroHookException {
    }

    public void onReady(Object b, BnNodeLocal n) throws HeroHookException {
    }

    public void beforeTerminate(Object b, BnNodeLocal n) throws HeroHookException {
    }
}
